/**
 * 公共的js文件
 * @author zlc
 */
window.onload=function() {
    layui.define(['element', 'form','laydate','table','upload','layedit','layer'], function (exports) {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        /**
         * 手动排序
         */
        $(document).on('blur', '.wy-input-blur', function () {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            var n_value = that.val();
            var o_value = data_json.def_value || '';
            if (n_value == o_value) {
                return;
            }
            if (data_json.zz) {
                var zz = new RegExp(data_json.zz);
                var zz_title = data_json.zz_title || "请输入有效信息";
                if (!zz.test(n_value)) {
                    layer.msg(zz_title);
                    return;
                }
            }
            $.post(data_json.url, {"val": n_value}, function (data) {
                if (data.code == 0) {
                    layer.msg(data.msg,{
                        icon:6,
                        time: 1000,
                        end: function () {
                            table.reload('dataTable',{ where: {}}); //只重载数据
                        }
                    });
                }else{
                    layer.msg(data.msg,{icon:5});
                }
            }, "JSON");
        });

        /**
         * 修改状态
         */
        form.on('checkbox(lay-status-checkbox)', function (data) {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            var n_value = that.val();
            $.post(data_json.url, {"id": n_value}, function (data) {
                if (data.code == 0) {
                    layer.msg(data.msg, {
                        icon:6,
                        time: 1000,
                        end: function () {
                            table.reload('dataTable',{ where: {}}); //只重载数据
                        }
                    });
                    return;
                } else {
                    layer.msg(data.msg,{
                        time: 1000,
                        end: function () {
                            table.reload('dataTable',{ where: {}}); //只重载数据
                        }
                    });
                    return;
                }
            }, "json");
            form.render('checkbox');
        });


        /**
         * 修改状态 切换按钮
         */
        form.on('switch(lay-status-switch)', function (data) {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            var n_value = that.val();
            $.post(data_json.url, {"id": n_value}, function (data) {
                if (data.code == 0) {
                    layer.msg(data.msg, {
                        icon:6,
                        time: 1000,
                        end: function () {
                            table.reload('dataTable',{ where: {}}); //只重载数据
                        }
                    });
                    return;
                } else {
                    layer.msg(data.msg);
                    return;
                }
            }, "json");
            form.render('checkbox');
        });

        /**
         * 单个删除数据
         */
        $(document).on('click', '.layui-data-delete', function () {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            layer.confirm('删除后，将永久无法恢复，确定删除吗?', function (index) {
                $.post(data_json.url, {_method: 'delete', id: data_json.id}, function (result) {
                    if (result.code == 0) {
                        table.reload('dataTable',{ where: {}}); //只重载数据
                        layer.close(index);
                        layer.msg(result.msg,{icon:6});
                    }else{
                        layer.close(index);
                        layer.msg(result.msg,{icon:5});
                    }
                },"json");
            });
        });

        /**
         * 图片预览
         */
        $(document).on('click', '.layui-img-click', function () {
            var img = $(this).attr("src");
            var title = $(this).attr("alt");
            if (!img) {
                return;
            }
            layer.photos({
                shade: 0.3,
                photos: {
                    "title": title,
                    "start": 0,
                    "data": [
                        {
                            "src": img,
                        }
                    ]
                }
                , anim: 5
            });
        });

        /**
         * 数据保存/修改提交
         */
        form.on('submit(formSubmitData)',function (data) {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            $.ajax({
                type: "post",
                url: data_json.url,
                data: data.field,
                dataType: "json",
                success: function(res){
                     if(res.code == 0){
                         layer.msg(res.msg, {
                             icon:6,
                             time: 1000,
                             end: function () {
                                 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                 parent.layer.close(index); //再执行关闭
                             }
                         });
                         return;
                     }else{
                         layer.msg(res.msg);
                         return;
                     }
                }
            });
        });
        //时间范围段
        $('.enet_daterange').each(function () {
            laydate.render({
                elem: this,
                type: 'datetime'
                , range: true
            });
        });

        //设置开始时间段
        $('.enet_daterange_limit').each(function () {
            var min_time = $(this).attr('lay-mintime');
            laydate.render({
                elem: this,
                type: 'datetime'
                , range: true,
                min:min_time
            });
        });

        // 年月日 时分秒
        $('.enet_datetime').each(function(){
            laydate.render({
                elem: this,
                type: 'datetime'
            });
        });

        // 年月日
        $('.enet_date').each(function(){
            laydate.render({
                elem: this,
                type: 'date'
            });
        });

        exports('global', {})
    });
}

/*
 * 列表搜索通用js
 * Description: 用于layui table数据表格的搜索，当参数的值为空时默认不传此参数 需要引入utils.js
 * @param <frm> 表单ID <table> layui数据表格
 * Author: donghui
 * Date: 2018-7-2
 */
function searchFormTable(frm,table,p=1){
    var frm = $("#"+frm);
    var obj = table.config.where;

    //select
    frm.find("select").each(function(){
        var s = $(this),
            name = s.attr("name");
        obj[name] = Utils.trim(s.val());
        if (obj[name] === '' || name === undefined) {
            delete obj[name]
        }
    });

    //input
    frm.find("input").each(function(){
        var t = $(this),
            name = t.attr("name");
        obj[name] = Utils.trim(t.val());
        if (obj[name] === '' || name === undefined) {
            delete obj[name]
        }
    });
    table.reload({
        where: obj
        ,page: {curr: p}
    });
    console.log(obj);
    return true;
}
